import requests
import json

class GitHubAPI:
    @staticmethod
    def get_latest_release(repo_url):
        # 提取 GitHub 仓库路径
        repo_path = repo_url.replace("https://github.com/", "").strip("/")
        api_url = f"https://api.github.com/repos/{repo_path}/releases/latest"

        # 调用 GitHub API 获取最新版本信息
        response = requests.get(api_url)
        if response.status_code == 200:
            return response.json()
        else:
            print(f"Failed to fetch release info: {response.status_code}")
            return None

class ReportGenerator:
    @staticmethod
    def generate_release_report(repo_url):
        release_info = GitHubAPI.get_latest_release(repo_url)
        if release_info:
            report = {
                "repository": repo_url,
                "latest_version": release_info.get("tag_name"),
                "release_name": release_info.get("name"),
                "published_at": release_info.get("published_at"),
                "release_notes": release_info.get("body", "No release notes provided.")
            }
            report_file = f"release_report_{report['latest_version']}.json"
            with open(report_file, "w") as file:
                json.dump(report, file, indent=4)
            print(f"Report generated: {report_file}")
        else:
            print("No release information available.")
